package cn.iune.common.response;

import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;

public class MapMessage {
	
	public static final String STATUSINFO_SUCCESS_FLAG = "success";
	public static final String STATUSINFO_SUCCESS_MSG = "操作成功！";
	public static final String STATUSINFO_SUCCESS_CODE = "0";
	public static final String STATUSINFO_SUCCESS_INFO = "http://www.iune.cn";
	
	public static final String STATUSINFO_FAILURE_FLAG = "failure";
	public static final String STATUSINFO_FAILURE_MSG = "操作失败！";
	public static final String STATUSINFO_FAILURE_CODE = "-1";
	public static final String STATUSINFO_FAILURE_INFO = "http://www.iune.cn";

	
	public MapMessage(){
		
	}
	
	public MapMessage(boolean success){
		
		if(success){
			this.statusCode = STATUSINFO_SUCCESS_CODE;
			this.statusMsg = STATUSINFO_SUCCESS_MSG;
			this.statusInfo = STATUSINFO_SUCCESS_INFO;
			this.statusFlag = STATUSINFO_SUCCESS_FLAG;
			
			this.code = Integer.parseInt(STATUSINFO_SUCCESS_CODE);
			this.msg = STATUSINFO_SUCCESS_MSG;

		}else{
			this.statusCode = STATUSINFO_FAILURE_CODE;
			this.statusMsg = STATUSINFO_FAILURE_MSG;
			this.statusInfo = STATUSINFO_FAILURE_INFO;
			this.statusFlag = STATUSINFO_FAILURE_FLAG;
			
			this.code = Integer.parseInt(STATUSINFO_FAILURE_CODE);
			this.msg = STATUSINFO_FAILURE_MSG;
		}
		
		this.billType = null;
		this.billTypeName = null;
		this.fID = null;
		this.billNo = null;
		
		this.setData(new JSONObject());
	}
	
	public MapMessage(String fID,String billNo,String billType,String billTypeName,String statusCode ,String statusMsg,String statusInfo ,String statusFlag/*, String systemCode*/){
		this.fID = fID;
		this.billNo = billNo;
		this.billType = billType;
		this.billTypeName = billTypeName;
		
		this.statusCode = statusCode;
		this.statusMsg = statusMsg;
		this.statusInfo = statusInfo;
		this.statusFlag = statusFlag;
		
		this.setData(new JSONObject());
		
		this.code = Integer.parseInt(statusCode);
		this.msg = statusMsg;
		
		//this.systemCode = systemCode;
	}
	
	private String fID;
	private String billNo;
	private String billType;
	private String billTypeName;
	
	private String statusCode;
	private String statusMsg;
	private String statusInfo;
	private String statusFlag;

	private String systemCode;
	
	private String sProcess;
	private String sName;

	private JSONObject data;
	
	private int code;
	private String msg;
	
	public JSONObject getData() {
		return data;
	}

	public void setData(JSONObject data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getfID() {
		return fID;
	}
	public void setfID(String fID) {
		this.fID = fID;
	}
	public String getBillNo() {
		return billNo;
	}
	public void setBillNo(String billNo) {
		this.billNo = billNo;
	}
	public String getBillType() {
		return billType;
	}
	public void setBillType(String billType) {
		this.billType = billType;
	}
	public String getBillTypeName() {
		return billTypeName;
	}
	public void setBillTypeName(String billTypeName) {
		this.billTypeName = billTypeName;
	}
	public String getStatusCode() {
		return statusCode;
	}
	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
		this.code = Integer.parseInt(statusCode);

	}
	public String getStatusMsg() {
		return statusMsg;
	}
	public void setStatusMsg(String statusMsg) {
		this.statusMsg = statusMsg;
		this.msg = statusMsg;

	}
	public String getStatusInfo() {
		return statusInfo;
	}
	public void setStatusInfo(String statusInfo) {
		this.statusInfo = statusInfo;
	}
	public String getStatusFlag() {
		return statusFlag;
	}
	public void setStatusFlag(String statusFlag) {
		this.statusFlag = statusFlag;
	}


	public String getsProcess() {
		return sProcess;
	}
	public void setsProcess(String sProcess) {
		this.sProcess = sProcess;
	}
	public String getsName() {
		return sName;
	}
	public void setsName(String sName) {
		this.sName = sName;
	}
	
	public String getSystemCode() {
		return systemCode;
	}
	public void setSystemCode(String systemCode) {
		this.systemCode = systemCode;
	}
	public void fromMap(Map<String,Object> map){
		
		Object fID = map.get("fID");
		Object billNo =  map.get("billNo");
		Object billType =  map.get("billType");
		Object billTypeName =  map.get("billTypeName");
		Object statusCode =  map.get("statusCode");
		Object statusMsg =  map.get("statusMsg");
		Object statusInfo =  map.get("statusInfo");
		Object statusFlag =  map.get("statusFlag");
		
		Object systemCode =  map.get("systemCode");
		Object sProcess =  map.get("sProcess");
		Object sName =  map.get("sName");
		
		JSONObject data = (JSONObject) map.get("data");
		
		this.setfID(fID==null?null:fID.toString());
		this.setBillNo(billNo==null?null:billNo.toString());
		this.setBillType(billType==null?null:billType.toString());
		this.setBillTypeName(billTypeName==null?null:billTypeName.toString());

		this.setStatusCode(statusCode==null?null:statusCode.toString());
		this.setStatusMsg(statusMsg==null?null:statusMsg.toString());
		this.setStatusInfo(statusInfo==null?null:statusInfo.toString());
		this.setStatusFlag(statusFlag==null?null:statusFlag.toString());
		
		this.setSystemCode(systemCode==null?null:systemCode.toString());
		this.setsProcess(sProcess==null?null:sProcess.toString());
		this.setsName(sName==null?null:sName.toString());
		

		this.setData(data);
		
		int code = Integer.parseInt(statusCode.toString());
		String msg = statusMsg.toString();

		this.setCode(code);
		this.setMsg(msg);

	}

	
	public Map<String,Object> toMap(){
		
		Map<String,Object> map =  new HashMap<String, Object>();
		
		map.put("fID", this.getfID());
		map.put("billNo", this.getBillNo());
		map.put("billType", this.getBillType());
		map.put("billTypeName", this.getBillTypeName());
		
		map.put("statusCode", this.getStatusCode());
		map.put("statusMsg", this.getStatusMsg());
		map.put("statusInfo", this.getStatusInfo());
		map.put("statusFlag", this.getStatusFlag());
		
		map.put("systemCode", this.getSystemCode());
		map.put("sProcess", this.getsProcess());
		map.put("sName", this.getsName());
		
		map.put("data", this.getData());
		
		map.put("code", this.getCode());
		map.put("msg", this.getMsg());
		
		return map;
	}
	
	public JSONObject toJSONObject(){
		
		/*		
		JSONObject json = new JSONObject();

		JSONObject status = new JSONObject();
		Set<Entry<String,Object>> entries = this.toMap().entrySet();
		for(Map.Entry<String,Object> entry : entries){
			if(!entry.getKey().equals("data")){
				status.put(entry.getKey(), entry.getValue());
			}
		}
		json.put("status",status);
		
		JSONObject data = (JSONObject) this.toMap().get("data");
		json.put("data", data);
		*/

		JSONObject json = (JSONObject) JSONObject.toJSON(this);

		return json;
	}

	
	public String toString(){
		String str = "billNo:"+this.getBillNo() + "\r\n"
		+ "billType:"+this.getBillType() + "\r\n"
		+ "billTypeName:"+this.getBillTypeName() + "\r\n"
		+ "statusCode:"+this.getStatusCode() + "\r\n"
		+ "statusMsg:"+this.getStatusMsg() + "\r\n"
		+ "statusInfo:"+this.getStatusInfo() + "\r\n"
		+ "statusFlag:"+this.getStatusFlag() + "\r\n"
		;
		return str;
	}	
	
}
